home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / debugger / ddd-1.000 / ddd-1 / ddd-1.4b / ddd / hostname.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-11  |  3.8 KB  |  167 lines

  1. // $Id: hostname.C,v 1.8 1996/01/11 17:33:07 zeller Exp $ -*- C++ -*-
  2. // Return `official' name of host
  3.  
  4. // Copyright (C) 1995 Technische Universitaet Braunschweig, Germany.
  5. // Written by Andreas Zeller (zeller@ips.cs.tu-bs.de).
  6. // 
  7. // This file is part of the DDD Library.
  8. // 
  9. // The DDD Library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU Library General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. // 
  14. // The DDD Library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. // See the GNU Library General Public License for more details.
  18. // 
  19. // You should have received a copy of the GNU Library General Public
  20. // License along with the DDD Library -- see the file COPYING.LIB.
  21. // If not, write to the Free Software Foundation, Inc.,
  22. // 675 Mass Ave, Cambridge, MA 02139, USA.
  23. // 
  24. // DDD is the data display debugger.
  25. // For details, see the DDD World-Wide-Web page, 
  26. // `http://www.cs.tu-bs.de/softech/ddd/',
  27. // or send a mail to the DDD developers at `ddd@ips.cs.tu-bs.de'.
  28.  
  29. char hostname_rcsid[] = 
  30.     "$Id: hostname.C,v 1.8 1996/01/11 17:33:07 zeller Exp $";
  31.  
  32. #include "hostname.h"
  33. #include "config.h"
  34.  
  35. #ifndef NO_UNAME_AGENTS
  36. #include "Agent.h"
  37. #endif
  38.  
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42.  
  43. extern "C" {
  44. #ifdef HAVE_SYS_TYPES_H
  45. #include <sys/types.h>
  46. #endif
  47.  
  48. #ifdef HAVE_SYS_SOCKET_H
  49. #include <sys/socket.h>
  50. #endif
  51.  
  52. #ifdef HAVE_SYS_UTSNAME_H
  53. #include <sys/utsname.h>
  54. #endif
  55.  
  56. #ifdef HAVE_NETDB_H
  57. #include <netdb.h>
  58. #endif
  59.  
  60. #if defined(HAVE_GETHOSTNAME) && !defined(HAVE_GETHOSTNAME_DECL)
  61.     int gethostname(char *name, int namelen);
  62. #endif
  63. #if defined(HAVE_UNAME) && !defined(HAVE_UNAME_DECL)
  64.     int uname(struct utsname *name);
  65. #endif
  66. }
  67.  
  68. // Return the host name
  69. char *hostname()
  70. {
  71.     static char *name = 0;
  72.     if (name)
  73.     return name;
  74.  
  75.     char buffer[BUFSIZ];
  76.  
  77.     bool okay = false;
  78.  
  79. #ifdef HAVE_UNAME
  80.     struct utsname un;
  81.     if (!okay && uname(&un) >= 0)
  82.     {
  83.     strcpy(buffer, un.nodename);
  84.     okay = true;
  85.     }
  86. #elif defined(HAVE_GETHOSTNAME)
  87.     if (!okay && gethostname(buffer, BUFSIZ) == 0)
  88.     {
  89.     okay = true;
  90.     }
  91. #endif
  92. #ifndef NO_UNAME_AGENTS
  93.     if (!okay)
  94.     {
  95.     Agent agent("uname -n");
  96.     agent.start();
  97.  
  98.     if (agent.inputfp())
  99.     {
  100.         buffer[0] = '\0';
  101.         fscanf(agent.inputfp(), "%s", buffer);
  102.     }
  103.     }
  104. #endif
  105.  
  106.     if (okay)
  107.     return name = strcpy(new char[strlen(buffer) + 1], buffer);
  108.     else
  109.     return name = "unknown";
  110. }
  111.  
  112. // Return a fully qualified name for the current host
  113. static char *_fullhostname(char *host)
  114. {
  115.     if (host == 0)
  116.     host = hostname();
  117.     if (strchr(host, '.'))
  118.     return host;        // HOST already qualified
  119.  
  120. #ifdef HAVE_GETHOSTBYNAME
  121.     struct hostent *h = gethostbyname(host);
  122.     if (h)
  123.     {
  124.     // Check official name
  125.     if (strchr(h->h_name, '.'))
  126.         return h->h_name;
  127.  
  128.     // Check aliases
  129.     for (int i = 0; h->h_aliases[i] != 0; i++)
  130.         if (strchr(h->h_aliases[i], '.'))
  131.         return h->h_aliases[i];
  132.  
  133.     // Use first network address
  134.     if (h->h_addrtype == AF_INET && h->h_addr_list[0] != 0)
  135.     {
  136.         static char buffer[128];
  137.         buffer[0] = '\0';
  138.         for (int i = 0; i < h->h_length; i++)
  139.         sprintf(buffer + strlen(buffer), i ? ".%d" : "%d",
  140.             int((unsigned char)(h->h_addr_list[0][i])));
  141.  
  142.         return buffer;
  143.     }
  144.     }
  145. #endif
  146.  
  147.     // Keep on using this host name
  148.     return host;
  149. }
  150.  
  151.  
  152. // Return and cache a fully qualified name for the current host
  153. char *fullhostname(char *host)
  154. {
  155.     // Buffer for local host name
  156.     static char *name = 0;
  157.  
  158.     if (name && host == 0)
  159.     return name;
  160.  
  161.     char *n = _fullhostname(host);
  162.     if (host == 0)
  163.     return name = strcpy(new char[strlen(n) + 1], n);
  164.     else
  165.     return n;
  166. }
  167.